﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace TestClient
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            l = new TestClient.Client();
            serveur = new TestClient.ServeurClientUPnP();
            button2.Visibility = Visibility.Hidden;
            
        }

        TestClient.ServeurClientUPnP serveur;
        TestClient.Client l;
        Boolean found;
        int counter;
        static public string nomdispo;

        static int compteur = 0;
        int bb = 1;
        

        private void RefreshMessage(string message)
        {
            Message.Text = message;
            Message.Refresh();
        }
        
        private void button1_Click(object sender, RoutedEventArgs e)
        {
             

             nomdispo = textBox1.Text;
            Console.WriteLine("nomdispo du dispositif est :" +nomdispo);
            serveur.startUPnP();

            
            found = false;
            counter = 0;
            try
           {
               RefreshMessage("Discovering...");

             do
             {

                l.Search();

                if (TestClient.Client.Zones.Count > 0)
                {

                    found = true;
                    Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ true ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
                    RefreshMessage("Discovered " + TestClient.Client.Zones[0].ToString() + " devices :  " + TestClient.Client.Zones[1].ToString());
                    counter = 4;
                    button2.Visibility = Visibility.Visible;

                }
                else
                {
                    found = false;
                    RefreshMessage("Discovering...");
                    Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ false ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
                    Console.WriteLine("Sleep for 5 seconds.");
                    Thread.Sleep(5000);
                    counter++;
                }
                }
            
             while (counter<4);

             Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ j'ai terminé ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
          }

            catch (Exception ex)
            {
                RefreshMessage(ex.Message + " Try discovery again.");
            }

            if (found == false)
            {
                RefreshMessage("No devices discovered. Try discovery again.");
            }

       
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            
            l.sendFile();

            RefreshMessage("File Sent To : " + TestClient.Client.Zones[1].ToString());

            image1.Visibility = Visibility.Hidden;
            stackPanel1.Visibility = Visibility.Hidden;
            button11.Visibility = Visibility.Hidden;
            button22.Visibility = Visibility.Hidden;
            progressBar1.Visibility = Visibility.Hidden;

        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            serveur.startUPnP();
        }
        /*
        private void button5_Click(object sender, RoutedEventArgs e)
        {
            serveur.stopUPnP();
        }
        */
        private void Next_Click(object sender, RoutedEventArgs e)
        {

            compteur++;

            bb++;
            changeValeur(compteur,bb);
                      
         }





        private void First_Click(object sender, RoutedEventArgs e)
        {
            compteur--;
            bb--;
            changeValeur(compteur,bb);
        }


        public void changeValeur(int w, int x) {

            if (w == 1)
            {
                
                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);

                ChangeImage("http://www.linternaute.com/nature-animaux/magazine/photo/les-100-paysages-de-france/image/etretat-262711.jpg", jj);

            }
            else if (compteur == 2)
            {


                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x+w;
                Console.WriteLine("jj esttttttttttt   : " + jj);

                ChangeImage("http://tigresse888.t.i.pic.centerblog.net/o/74184583.jpg", jj);
            }
            else if (compteur == 3)
            {


                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);
                ChangeImage("http://www.midilibrevoyages.com/imgfr/europe/autriche-tyrol-2.jpg", jj);
            }
            else if (compteur == 4)
            {


                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);
                ChangeImage("http://www.interalpen.com/image/medium/fr/sejour-motivation-hiver-autriche.jpg", jj);

            }
            else if (compteur == 5)
            {

                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);
                ChangeImage("http://www.consigliando.it/wp-content/uploads/2011/01/amsterdam.jpg", jj);

            }
            else if (compteur == 6)
            {


                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);

                ChangeImage("http://img0.svstatic.com/wallpapers/8c73c2932f94f48f44bc8fa8fb47c9aa_large.jpeg", jj);

            }
            else if (compteur == 7)
            {


                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);
                ChangeImage("http://faistoilabelle.files.wordpress.com/2011/04/venise_940x705.jpg", jj);
            }

            else if (compteur == 8)
            {

                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);

                ChangeImage("http://photos.hotelsclick.com/TN/HAMM/46780/5.jpg", jj);

            }
            else if (compteur == 9)
            {

                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);
                ChangeImage("http://blog.govoyages.com/wp-content/uploads/grece-crete.jpg", jj);

            }
            else if (compteur == 10)
            {

                Console.WriteLine("bbbbbbbbbbbbbbb " + x);
                int jj = x + w;
                Console.WriteLine("jj esttttttttttt   : " + jj);
                ChangeImage("http://img.1.vacanceo.net/classic/207520.jpg", jj);

            }

            else if (compteur > 10)
            {

                bb = 2;
                compteur = 1;
                changeValeur(compteur,bb);


            }
            else if (compteur < 1)
            {

                bb = 11;
                compteur = 10;
                changeValeur(compteur,bb);
            }
        
        
        
        }





        public void ChangeImage(string nonImg, int cmp) {


            Binding tmpBinding = new Binding();
            
            tmpBinding.Source = new Uri(@nonImg, UriKind.Absolute);

            image1.SetBinding(Image.SourceProperty, tmpBinding);
            progressBar1.Value = cmp;
        
        }













        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                Console.WriteLine("Hamdouuuuuuuuuuuuuuuuuuuuuulaaaaaaaaaaaaaaaaaahhhh");
                serveur.startUPnP();


                found = false;
                counter = 0;
                try
                {
                    RefreshMessage("Discovering...");

                    do
                    {

                        l.Search();

                        if (TestClient.Client.Zones.Count > 0)
                        {

                            found = true;
                            Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ true ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
                            RefreshMessage("Discovered " + TestClient.Client.Zones[0].ToString() + " devices :  " + TestClient.Client.Zones[1].ToString());
                            counter = 4;
                            button2.Visibility = Visibility.Visible;

                        }
                        else
                        {
                            found = false;
                            RefreshMessage("Discovering...");
                            Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ false ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
                            Console.WriteLine("Sleep for 5 seconds.");
                            Thread.Sleep(5000);
                            counter++;
                        }
                    }

                    while (counter < 4);

                    Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ j'ai terminé ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
                }

                catch (Exception ex)
                {
                    RefreshMessage(ex.Message + " Try discovery again.");
                }

                if (found == false)
                {
                    RefreshMessage("No devices discovered. Try discovery again.");
                }
            
            }

            else if (comboBox1.SelectedIndex == 1)
            {

                Console.WriteLine("ya Rabiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii");
                l.sendFile();

                RefreshMessage("File Sent To : " + TestClient.Client.Zones[1].ToString());
            
             }
        }

                

        
    }























        public static class ExtensionMethods
        {
            private static Action EmptyDelegate = delegate() { };

            public static void Refresh(this UIElement uiElement)
            {
                uiElement.Dispatcher.Invoke(EmptyDelegate, System.Windows.Threading.DispatcherPriority.Render);
            }
        }
   
}
